package com.onlinemeal.customer.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlinemeal.customer.dao.CustomerDao;
import com.onlinemeal.customer.po.Customer;

@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
	@Autowired(required=true)
//	private EntityManagerFactory entityManagerFactory;
	@PersistenceContext(name="unitName")
	private EntityManager entityManager;
	
	@Override
	public int addCustomer(Customer customer) {
		System.out.println("dao");
//		entityManagerFactory = Persistence.createEntityManagerFactory("OnlineMealOrdering");
//		entityManager = entityManagerFactory.createEntityManager();
//		EntityTransaction transaction = entityManager.getTransaction();
//		transaction.begin();
		entityManager.persist(customer);
//		transaction.commit();
		entityManager.close();
		return 0;
	}
	
	@Override
	public Customer queryCustomerByID(String id) {
//		entityManager = entityManagerFactory.createEntityManager();
		
		return null;
	}

	@Override
	public int deleteCustomer(Customer customer) {
//		entityManager = entityManagerFactory.createEntityManager();
		
		return 0;
	}

	@Override
	public Customer findByCode(String code) {
//		entityManagerFactory = Persistence.createEntityManagerFactory("OnlineMealOrdering");
//		entityManager = entityManagerFactory.createEntityManager();
		String jpql="select customer from com.onlinemeal.customer.po.Customer customer where customer.code=:code";
		@SuppressWarnings("unchecked")
		List<Customer> customer_list = entityManager.createQuery(jpql).setParameter("code", code).getResultList();
		if(customer_list.isEmpty()) {
			return null;
		} else {
			Customer customer = customer_list.get(0);
			entityManager.close();
			return customer;
		}
	}

	@Override
	public void updateState(String uid, String state) {
//		entityManagerFactory = Persistence.createEntityManagerFactory("OnlineMealOrdering");
//		entityManager = entityManagerFactory.createEntityManager();
		Customer customer = entityManager.find(Customer.class, uid);
//		EntityTransaction transaction = entityManager.getTransaction();
//		transaction.begin();
		customer.setState("1");
//		transaction.commit();
		entityManager.close();
	}

	@Override
	public Customer findByUsernameAndPassword(Customer customer) {
//		entityManagerFactory = Persistence.createEntityManagerFactory("OnlineMealOrdering");
//		entityManager = entityManagerFactory.createEntityManager();
		
		String jpql="select customer from com.onlinemeal.customer.po.Customer customer where customer.username=:username and customer.password=:password";
		@SuppressWarnings("unchecked")
		List<Customer> customer_list = entityManager.createQuery(jpql).setParameter("username", customer.getUsername()).setParameter("password", customer.getPassword()).getResultList();
		if(customer_list.isEmpty()) {
			entityManager.close();
			return null;
		} else {
			Customer customer_find = customer_list.get(0);
			entityManager.close();
			return customer_find;
		}
	}

}
